package com.sleepcamel.resteasyConfigurator.components.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;

import com.sleepcamel.rest.domain.BaseMethod;
import com.sleepcamel.rest.domain.Mapping;
import com.sleepcamel.rest.domain.Project;
import com.sleepcamel.rest.domain.ProjectPackage;
import com.sleepcamel.rest.domain.method.RESTMethod;
import com.sleepcamel.resteasyConfigurator.components.tree.strategies.PackageTreeLabelStrategy;
import com.sleepcamel.resteasyConfigurator.util.ImageDescriptorProvider;

public class ProjectTreeLabelProvider implements ILabelProvider {

	private List<ILabelProviderListener> listeners;
	private Map<ImageDescriptor,Image> imageCache = new HashMap<ImageDescriptor, Image>();
	private ITreeLabelStrategy labelStrategy = new PackageTreeLabelStrategy();

	public ProjectTreeLabelProvider() {
		listeners = new ArrayList<ILabelProviderListener>();
	}

	@Override
	public void addListener(ILabelProviderListener arg0) {
		listeners.add(arg0);
	}

	@Override
	public void removeListener(ILabelProviderListener arg0) {
		listeners.remove(arg0);
	}

	public ITreeLabelStrategy getLabelStrategy() {
		return labelStrategy;
	}

	public void setLabelStrategy(ITreeLabelStrategy labelStrategy) {
		this.labelStrategy = labelStrategy;
	}

	public Image getImage(Object element) {
		ImageDescriptor descriptor = null;
		if (element instanceof Project) {
			descriptor = ImageDescriptorProvider.getImageDescriptor("project-open.png");
		} else if (element instanceof ProjectPackage) {
			descriptor = ImageDescriptorProvider.getImageDescriptor("container_object.png");
		} else if (element instanceof Mapping) {
			descriptor = ImageDescriptorProvider.getImageDescriptor("java.png");
		} else if (element instanceof RESTMethod) {
			descriptor = ImageDescriptorProvider.getImageDescriptor("hand"+((RESTMethod) element).getType()+".png");
		} else if (element instanceof BaseMethod) {
			descriptor = ImageDescriptorProvider.getImageDescriptor("method_public_object.png");
		} else {
			descriptor = ImageDescriptorProvider.getImageDescriptor("nofile.png");
		}
		
		// obtain the cached image corresponding to the descriptor
		Image image = (Image) imageCache.get(descriptor);
		if (image == null) {
			image = descriptor.createImage();
			imageCache.put(descriptor, image);
		}
		return image;
	}

	public void dispose() {
		for (Iterator<Image> i = imageCache.values().iterator(); i.hasNext();) {
			((Image) i.next()).dispose();
		}
		imageCache.clear();
	}

	@Override
	public boolean isLabelProperty(Object arg0, String arg1) {
		return labelStrategy.isLabelProperty(arg0, arg1);
	}

	@Override
	public String getText(Object arg0) {
		return labelStrategy.getText(arg0);
	}

}
